#!/system/bin/sh
# @Version=1
# Copyright 2008 parrothd
# Copyright 2009 zodiac (Markus Gutschke)

tetherlog=/data/data/android.tether/var/tether.log
mac_whitelist=/data/data/android.tether/conf/whitelist_mac.conf
dnsmasq_pidfile=/data/data/android.tether/var/dnsmasq.pid
dnsmasq_leasefile=/data/data/android.tether/var/dnsmasq.leases

run() {
    if eval { "$1;" } >/dev/null 2>&1; then
      local i
      echo -n "<div class=\"date\">`date +"%d.%m.%y %T"`</div><div class=\"action\">$2...</div><div class=\"output\">" >>"$tetherlog"
      if i=`eval "$3" 2>&1`; then
        echo "</div><div class=\"done\">done</div><hr>" >>"$tetherlog"
      else
        { echo "$i" | while read i; do echo -n "$i<br />"; done
        echo "</div><div class=\"failed\">failed</div><hr>"; } >>"$tetherlog"
      fi
    fi
}

startwifi() {
    ## Stop current users
    stopwifi

    ## Loading wlan.ko module
    run '!({ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done)' \
        "Loading wlan.ko module" \
        'insmod /system/lib/modules/wlan.ko'

    ## Configuring WiFi
    run : \
        "Configuring WiFi interface" \
        'wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e /proc/calibration -i /data/data/android.tether/conf/tiwlan.ini'
}

stopwifi() {
    ## Stopping current users of the wifi interface
    run 'ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*|*wpa_supplicant*|*dhcp*) break;; esac; false; done' \
        "Stopping current WiFi users" \
        '{ ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*|*wpa_supplicant*|*dhcp*) kill "$p";; esac; done; :; }'

    ## Configuring WiFi
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Configuring WiFi interface" \
        'wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e /proc/calibration -i /system/etc/wifi/tiwlan.ini'

    # Unloading module
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Unloading wlan.ko module" \
        'rmmod wlan'
}

startint() {
    # Configuring network interface
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Configuring network interface" \
        'ifconfig tiwlan0 192.168.2.254 netmask 255.255.255.0 && ifconfig tiwlan0 up'
}

stopint() {
    # Shutting down network interface
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Shutting down network interface" \
        'ifconfig tiwlan0 down'
}

startipt() {
    ## Setting up firewall rules
    run '{ echo; lsmod; } | while read m; do case "$m" in wlan*) break;; esac; false; done' \
        "Setting up firewall rules" \
        '/data/data/android.tether/bin/iptables -F &&
         /data/data/android.tether/bin/iptables -F -t nat &&
         /data/data/android.tether/bin/iptables -I INPUT -s 192.168.2.0/24 -j DROP &&
         /data/data/android.tether/bin/iptables -I INPUT -p 17 -s 192.168.2.0/24 -m multiport --ports 53,67,68,1024:65535 -j ACCEPT &&
         /data/data/android.tether/bin/iptables -I INPUT -p 6 -s 192.168.2.0/24 -m multiport --ports 53,67,68,1024:65535 -j ACCEPT &&
         /data/data/android.tether/bin/iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT &&
         /data/data/android.tether/bin/iptables -I FORWARD -s 192.168.2.0/24 -j ACCEPT &&
         /data/data/android.tether/bin/iptables -P FORWARD DROP &&
         /data/data/android.tether/bin/iptables -t nat -I POSTROUTING -s 192.168.2.0/24 -j MASQUERADE'
}

stopipt() {
    ## Tearing down firewall rules
    run :\
        "Tearing down firewall rules" \
        '/data/data/android.tether/bin/iptables -F &&
         /data/data/android.tether/bin/iptables -F -t nat &&
         /data/data/android.tether/bin/iptables -P FORWARD ACCEPT'
}

startipfw() {
    ## Enabling IP forwarding
    run 'case "`cat /proc/sys/net/ipv4/ip_forward`" in 1) false;; esac' \
        "Enabling IP forwarding" \
        '{ echo 1 > /proc/sys/net/ipv4/ip_forward; }'
}
stopipfw() {
    ## Disabling IP forwarding
    run 'case "`cat /proc/sys/net/ipv4/ip_forward`" in 0) false;; esac' \
        "Disabling IP forwarding" \
        '{ echo 0 > /proc/sys/net/ipv4/ip_forward; }'
}

startdnsmasq() {
    ## Starting dnsmasq
    run '!(ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*) break;; esac; false; done)' \
        "Starting dnsmasq" \
        '/data/data/android.tether/bin/dnsmasq --conf-file=/data/data/android.tether/conf/dnsmasq.conf'
}

stopdnsmasq() {
    ## Stopping dnsmasq
    run 'ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*) break;; esac; false; done' \
        "Stopping dnsmasq" \
        'ps | while read u p pp v r w pc s n; do case "$n" in *dnsmasq*) kill "$p";; esac; done; rm '"'$dnsmasq_pidfile' 2>/dev/null"
}

startsecnat() {
    ## Activating MAC access control
    run "dd if='$mac_whitelist' >/dev/null 2>&1" \
        "Activating MAC access control" \
        '/data/data/android.tether/bin/iptables -t nat -I PREROUTING -s 192.168.2.0/24 -j DROP'
}

startmacwhitelist() {
    ## Adding MAC addresses for allowed clients
    run "(read i <'$mac_whitelist') 2>/dev/null" \
        "Adding MAC addresses for allowed clients" \
        "(while read mac; do /data/data/android.tether/bin/iptables -t nat -I PREROUTING -m mac --mac-source \$mac -j ACCEPT || exit 1; done) <'$mac_whitelist'"
}

case "$1" in
'start')
    rm "$tetherlog" 2>/dev/null
    startwifi
    startint
    startipt
    startipfw
    startdnsmasq
    startsecnat
    startmacwhitelist
;;
'stop')
    rm "$tetherlog" 2>/dev/null
    rm "$dnsmasq_leasefile" 2>/dev/null
    stopdnsmasq
    stopint
    stopipfw
    stopipt
    stopwifi
;;
'restartsecwifi')
    stopipt
    startipt
    startsecnat
    startmacwhitelist
;;
'test')
    startsecnat
;;
*)
echo "Usage: $0 [start|stop]"
;;
esac

